package cn.ican.viphone.activity;

import java.util.ArrayList;

import cn.ican.viphone.activity.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;

public class NewGuideActivity extends Activity{
	private ViewPager viewPager;
	private ArrayList<View> pageViews;
	private ImageView[] imageViews;
	
	private ViewGroup newguide;
	private ViewGroup dotgroup;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		LayoutInflater inflater = getLayoutInflater();
		
		pageViews = new ArrayList<View>();
		pageViews.add(inflater.inflate(R.layout.newguide_0, null));
		pageViews.add(inflater.inflate(R.layout.newguide_1, null));
		pageViews.add(inflater.inflate(R.layout.newguide_2, null));
		pageViews.add(inflater.inflate(R.layout.newguide_3, null));
		
		newguide = (ViewGroup)inflater.inflate(R.layout.newguide_main, null);
		dotgroup = (ViewGroup)newguide.findViewById(R.id.dotguide);
		viewPager = (ViewPager)newguide.findViewById(R.id.guideViewPager);
		
		imageViews = new ImageView[pageViews.size()];
		for (int i = 0; i < pageViews.size(); i++){
			ImageView image = new ImageView(NewGuideActivity.this);
			image.setLayoutParams(new LayoutParams(20, 20));
			image.setPadding(20, 0, 20, 0);
			image.setBackgroundResource(i == 0 ? R.drawable.dot_focused : R.drawable.dot);
			imageViews[i] = image;
			dotgroup.addView(image);
		}
		setContentView(newguide);
		viewPager.setAdapter(new guidePageAdapter());
		viewPager.setOnPageChangeListener(new guidePageChangeListener());
	}

	public void inputHome(View v){
		System.out.println("input home!");
		Intent homeIntent = new Intent(this, HomeActivity.class);
		startActivity(homeIntent);	
	}
	
	class guidePageAdapter extends PagerAdapter{
		public void finishUpdate(View arg0) {

		}
		
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}
		
		public Parcelable saveState() {
			return null;
		}

		public void startUpdate(View arg0) {		
		}

		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager)arg0).removeView(pageViews.get(arg1));
		}

		public int getCount() {
			return pageViews.size();
		}

		public Object instantiateItem(View arg0, int arg1) {
			View v = pageViews.get(arg1);
			((ViewPager)arg0).addView(v);
			return v;
		}

		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}
	
	class guidePageChangeListener implements OnPageChangeListener {
		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int arg0) {
			for (int i = 0; i < imageViews.length; i++){
				imageViews[i].setBackgroundResource(i == arg0 ? R.drawable.dot_focused : R.drawable.dot);
			}
		}
		
	}
}
